Crate phi_detector

source ·
Expand description

This is an implementation of Phi Accrual Failure Detector.

To reduce the memory footprint, pings or intervals aren’t actually stored but only two values to calculate normal distribution are maintained. This not only reduces the memory footprint to the constant value but also the computational cost for each ping down to constant.

Why does the memory footprint matter? Think about your application communicates with thousand of remote servers and you want to maintain failure detector for each server. Apparently, it is too expensive to cost 100MB for a failure detector per server.

use phi_detector::PingWindow;
use std::time::Duration;

// Create a window with a interval 100ms.
let mut window = PingWindow::new(Duration::from_millis(100));

window.add_ping(Duration::from_millis(150));
window.add_ping(Duration::from_millis(80));
// Now the window has intervals [100ms, 150ms, 80ms]. Average is 110ms.

let normal_dist = window.normal_dist();
// If the server is down for 5s, it should be failure.
let phi = normal_dist.phi(Duration::from_millis(5000));
if phi > 12. {
    panic!("The server is down");
}

Structs§